package com.leilei;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lei
 * @version 1.0
 * @date 2021/4/10 21:32
 * @desc map 算子
 */
public class MapOperator {
    public static void main(String[] args) throws Exception {
        List<String> stringList = Arrays.asList("a", "b", "c");
        List<String> mapChangeList = stringList.stream().map(String::toUpperCase).collect(Collectors.toList());
        // [A, B, C]
        System.out.println(mapChangeList);


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 设置并行度为1 （1个线程执行，以便观察）
        env.setParallelism(1);
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        // 加载数据源
        DataStreamSource<String> stream = env.fromCollection(stringList);
        // 使用map算子
        SingleOutputStreamOperator<String> source = stream.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value.toUpperCase() + "aa";
            }
        });
        // Aaa Baa Caa
        source.print();
        env.execute();
    }
}
